import os
from PIL import Image

def resize_images_in_directory(directory, factor=0.1):
    # 遍历目录中的所有文件
    for filename in os.listdir(directory):
        if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
            filepath = os.path.join(directory, filename)
            with Image.open(filepath) as img:
                # 计算新的尺寸
                new_width = int(img.width * factor)
                new_height = int(img.height * factor)

                # 重设图片尺寸
                img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)

                # 计算新的文件名
                new_filename = f"resized_{filename}"
                new_filepath = os.path.join(directory, new_filename)

                # 保存修改后的图片
                img.save(new_filepath)
                print(f"Resized and saved {new_filename}")

# 指定要处理的目录
directory_path = 'D:/mm_picture/test01'
resize_images_in_directory(directory_path)